import AttributeUtil from '@/AttributeUtil';
import MathUtil from '@/MathUtil';
import RandomUtil from '@/RandomUtil';
import UnitCommon from './UnitCommon';

declare global {
    interface SummonConfig {
        unitTypeId: string;
        caster: unit;
        x?: number;
        y?: number;
        count?: number;
        facing?: number;
        lifeTime?: number;
        attr?: AppAttribute;
        abilityId?: number | string;
        effect?: string;
        radius?: number;
    }
}
export default class SummonCommon {
    static summon(cfg: SummonConfig) {
        let { caster, abilityId, lifeTime, attr, count } = cfg;
        let dur = 0;
        let forever = 0;
        let ca = AttributeUtil.getUnitAttribute(caster);
        if (count == null) {
            count = 1;
        }
        if (ca != null) {
            // 计算时长增量
            dur += (ca[abilityId + '_effect_dur'] as number) ?? 0;
            forever = (ca[abilityId + '_sum_forever'] as number) ?? 0;
            // 数量增量
            count += (ca[abilityId + '_effect_count'] as number) ?? 0;
        }

        // 计算属性增量
        if (attr == null) {
            attr = {};
        }
        let p = GetOwningPlayer(cfg.caster);
        let us: unit[] = [];

        for (let i = 0; i < count; i++) {
            let { x, y, radius } = cfg;
            let nx = x,
                ny = y;
            if (count > 1) {
                let { x: tx, y: ty } = MathUtil.polarProjection(x, y, RandomUtil.nextInt(0, radius), RandomUtil.randomAngle());
                nx = tx;
                ny = ty;
            }
            let u = CreateUnit(p, cfg.unitTypeId, nx ?? GetUnitX(cfg.caster), ny ?? GetUnitY(cfg.caster), cfg.facing ?? GetUnitFacing(cfg.caster));
            us.push(u);

            attr.life = (attr.life ?? 0) + (ca?.宠物生命 ?? 0);
            // 生命增量
            attr.life_p = (attr.life_p ?? 0) + (ca?.宠物生命百分比 ?? 0) + ((ca?.[abilityId + '_sum_life_p'] as number) ?? 0);
            // 伤害增量
            attr.物理伤害倍率 = (ca?.宠物伤害增强 ?? 0) + ((ca?.[abilityId + '_sum_damage'] as number) ?? 0);
            attr.法术伤害倍率 = (ca?.宠物伤害增强 ?? 0) + ((ca?.[abilityId + '_sum_damage'] as number) ?? 0);
            // 防御增量
            attr.护甲百分比 = (attr.护甲百分比 ?? 0) + ((ca?.[abilityId + '_sum_def'] as number) ?? 0);
            attr.魔抗百分比 = (attr.魔抗百分比 ?? 0) + ((ca?.[abilityId + '_sum_def'] as number) ?? 0);

            let fp = UnitCommon.getFullProperty(caster);
            let phy = UnitCommon.取总力量(caster) + UnitCommon.取总敏捷(caster);
            let int = UnitCommon.取总智力(caster) + UnitCommon.取总精神(caster);

            attr.物理暴击机率 = phy / fp;
            attr.物理暴击伤害 = (phy * 2) / fp;
            attr.法术暴击机率 = int / fp;
            attr.法术暴击伤害 = (int * 2) / fp;

            AttributeUtil.addUnitAttribute(u, attr);

            if (cfg.effect && cfg.effect.length > 0) {
                DestroyEffect(AddSpecialEffectTarget(cfg.effect, u, 'origin'));
            }
            if (lifeTime && forever != 1) {
                UnitApplyTimedLife(u, 'Bklz', cfg.lifeTime + dur);
            }
        }

        return us;
    }
}
